package com.grandbroad.eim.ui.fragment;

import android.annotation.SuppressLint;
import android.os.Bundle;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.support.v4.widget.SwipeRefreshLayout;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import com.easy.west.westclient.api.hotvod.bean.VodList;
import com.easy.west.westclient.api.vod.event.GetVodListByCidEvent;
import com.grandbroad.eim.R;
import com.grandbroad.eim.adapter.VodListAdpter;
import com.grandbroad.eim.app.MyApplication;
import com.grandbroad.eim.base.BaseRecyclerview;

import org.greenrobot.eventbus.EventBus;
import org.greenrobot.eventbus.Subscribe;
import org.greenrobot.eventbus.ThreadMode;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.Unbinder;


/**
 * 通过bundle来判断
 */
public class VodFragment extends Fragment {

    @BindView(R.id.recycler_view)
    RecyclerView recyclerView;
    @BindView(R.id.base_recyclerview)
    SwipeRefreshLayout baseRecyclerview;
    Unbinder unbinder;
    private int cid;
    private VodListAdpter vodListAdpter;


    @Override
    public void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

    }

    @Nullable
    @Override
    public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.fragment_recyclerview, container, false);
        unbinder = ButterKnife.bind(this, view);
        initdata();
        return view;
    }

    private void initdata() {
        cid = getArguments().getInt("class_id");
        MyApplication.getMoleclient().getVodByCid(cid);
        vodListAdpter = new VodListAdpter((AppCompatActivity) getActivity());
        recyclerView.setLayoutManager(new LinearLayoutManager(getContext()));
        recyclerView.setAdapter(vodListAdpter);
    }

    @Override
    public void onStart() {
        super.onStart();
        if (!EventBus.getDefault().isRegistered(this)) {
            EventBus.getDefault().register(this);
        }
    }


    @Override
    public void onStop() {
        super.onStop();
        EventBus.getDefault().unregister(this);
    }

    @Override
    public void onDestroyView() {
        super.onDestroyView();
        unbinder.unbind();
    }

    /**
     * 获取列表
     * @param eva
     */
    @Subscribe(threadMode = ThreadMode.MAIN)
    public void getVodlist(GetVodListByCidEvent eva)
    {
        if (eva.isOk())
        {
            VodList list = eva.getBean();
            if (list.getStatus()==1)
            {
                vodListAdpter.setVodList(list.getData());
                vodListAdpter.notifyDataSetChanged();
            }
        }
    }

}
